<?php
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */
use App\Common\DbSelector;
use App\Lib\Consul\ServiceConfig;
use App\Http\Limter\RateLimiter;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Swoft\Crontab\Process\CrontabProcess;
use Swoft\Db\Pool;
use Swoft\Http\Server\HttpServer;
use Swoft\Task\Swoole\SyncTaskListener;
use Swoft\Task\Swoole\TaskListener;
use Swoft\Task\Swoole\FinishListener;
use Swoft\Rpc\Client\Client as ServiceClient;
use Swoft\Rpc\Client\Pool as ServicePool;
use Swoft\Rpc\Server\ServiceServer;
use Swoft\Http\Server\Swoole\RequestListener;
use Swoft\WebSocket\Server\WebSocketServer;
use Swoft\Server\SwooleEvent;
use Swoft\Db\Database;
use Swoft\Redis\RedisDb;
use Swoft\Consul\Consul;

return [
    'JwtHelper'=>[
        'class'=>\App\Helper\JwtHelper::class,
        'issuer'=>'http://www.99meitan.com',
        'audience'=>'http://www.99meitan.com',
        'exp'=>config('service.jwt_exp', 3600),
        'key'=>'YouShiErMeiShiErJiaGeMi',
        'signer'=>new Sha256()
    ],
    'noticeHandler'      => [
        'logFile' => '@runtime/logs/notice-%d{Y-m-d-H}.log',
    ],
    'applicationHandler' => [
        'logFile' => '@runtime/logs/error-%d{Y-m-d}.log',
    ],
    'logger'            => [
        'flushRequest' => false,
        'enable'       => false,
        'json'         => false,
    ],
    'httpServer'        => [
        'class'    => HttpServer::class,
        'port'     => config("service.http_service_port",18306),
        'listener' => [
            'rpc' => bean('rpcServer'),
        ],
        'on'       => [
//            SwooleEvent::TASK   => bean(SyncTaskListener::class),  // Enable sync task
            SwooleEvent::TASK   => bean(TaskListener::class),  // Enable task must task and finish event
            SwooleEvent::FINISH => bean(FinishListener::class)
        ],
        /* @see HttpServer::$setting */
        'setting' => [
            'task_worker_num'       => 12,
            'task_enable_coroutine' => true,
            'worker_num'            => 6
        ]
    ],
    'httpDispatcher'    => [
        // Add global http middleware
        'middlewares'      => [
            \App\Http\Middleware\FavIconMiddleware::class,
            \Swoft\Http\Session\SessionMiddleware::class,
            // \Swoft\Whoops\WhoopsMiddleware::class,
            // Allow use @View tag
            \Swoft\View\Middleware\ViewMiddleware::class,
            \App\Http\Middleware\CorsMiddleware::class,
        ],
        'afterMiddlewares' => [
            \Swoft\Http\Server\Middleware\ValidatorMiddleware::class
        ]
    ],
    'db'                => [
        'class'    => Database::class,
        'dsn'      => config('database.mysql_dsn'),
        'username' => config('database.mysql_username'),
        'password' => config('database.mysql_password'),
        'charset' => 'utf8mb4',
        'dbSelector'=>bean(DbSelector::class)
    ],
    'db.pool' => [
        'class'       => \Swoft\Db\Pool::class,
        'database'    => \bean('db'),
        'minActive'   => config('database.mysql_minActive', 10),
        'maxActive'   => config('database.mysql_maxActive', 20),
        'maxWait'     => config('database.mysql_maxWait', 10),
        'maxWaitTime' => config('database.mysql_maxWaitTime', 10),
        'maxIdleTime' => config('database.mysql_maxIdleTime', 60),
    ],
    'redis'      => [
        'class'    => RedisDb::class,
        'driver'   => 'phpredis',
        'host'     => config('database.redis_host', ''),
        'port'     => config('database.redis_port', 6379),
        'database' => config('database.redis_database', 32),
        'password' => config('database.redis_password', ''),
    ],
    'redis.pool'     => [
        'class'   => \Swoft\Redis\Pool::class,
        'redisDb' => \bean('redis'),
        'minActive'   => config('database.redis_minActive', 10),
        'maxActive'   => config('database.redis_maxActive', 20),
        'maxWait'     => config('database.redis_maxWait', 10),
        'maxWaitTime' => config('database.redis_maxWaitTime', 10),
        'maxIdleTime' => config('database.redis_maxIdleTime', 60),
    ],
    'consul'=>[
        'class'=>\Swoft\Consul\Consul::class,
        'host'=>config("service.consul_address",'192.168.0.189'),
        'port'=>config("service.consul_port",8500),
    ],
    'apollo' => [
        'class'=>\Swoft\Apollo\Apollo::class,
        'host' =>env('APOLLO_HOST', '47.93.220.163'),
        'port'=>env('APOLLO_PORT', 38080),
        'appId'=>'Gateway',
        'clusterName'=>env('APP_ENV', 'local')
    ],
    //diy
    'rpcServer'         => [
        'class' => ServiceServer::class,
        'port' => config('service.rpc_service_port', 18307),
        'setting' => [
            'worker_num'            => 6,
            'maxWaitTime' => 5
        ]
    ],
    'migrationManager'  => [
        'migrationPath' => '@database/Migration',
    ],
    'ServiceConfig'=>[
        'class'=>ServiceConfig::class,
        'name'=>config('service.service_name', 'Gateway'),
        'port'=>config('service.rpc_service_port', 18307),
        'prefix'=>config('service.service_prefix', 'Common'),
    ],
];
